import math
import os
import sys
import re

class LogFileAnalyzer:
    def __init__(self,filename=''):
        self.__eval_stamp=100000
        self.__setStampIndex=set()
        self.filename=filename
        if filename:
            self.parseLogFile()
            self.detectDistancePrecisionEvents()
            self.detectFitnessPrecisionEvents()
            self.sampleEvents()
            self.writeAnalyzeFile()
    def parseLogFile(self,filename=''):
        if not filename:filename=self.filename
        d={}
        it=[]
        v={}
        info=''
        answer=''
        l=open(filename).readlines()
        flag=0
        for i in l:
            if i[:2]=="/*":
                flag+=1
                continue
            if i[:2]=="*/":
                flag-=1
                continue
            if flag>0: continue
            if i[0]=='-':continue
            p=i.split(':')
            if len(p)==2:
                info+=i
                d[p[0].strip()]=p[1].strip()
            elif len(p)==3:
                info+=i
                d[p[0].strip()]=p[2].strip()
            else:
                p=i.split('=')
                if len(p)>1:
                    v[p[0].strip()]=p[1].strip()
                    answer+=i
                else:
                    it.append(i.strip().split())
        self.strInfo=info
        self.strAnswer=answer
        self.dicParameter=d
        self.lstEvaluation=[int(i) for i,j,k in it]
        self.lstFitness=[float(j) for i,j,k in it]
        self.lstDistance=[float(k) for i,j,k in it]
        self.lstIteration=it
        self.dicAnswer=v

    def detectDistancePrecisionEvents(self):
        precision_event=[]
        e=1
        st=self.lstEvaluation
        dist=self.lstDistance
        for i in range(1,11):
            n=0
            e*=.1
            flag=0
            for j in dist:
                if j<e:
                    precision_event.append(st[n])
                    self.__setStampIndex.add(n)
                    flag=1
                    break
                n+=1
            if flag==0:
                precision_event.append("")
        self.lstDistancePrecisionEvent=precision_event

    def detectFitnessPrecisionEvents(self):
        precision_event=[]
        e=1
        st=self.lstEvaluation
        dist=self.lstFitness
        for i in range(1,11):
            n=0
            e*=.1
            flag=0
            for j in dist:
                if j<e:
                    precision_event.append(st[n])
                    self.__setStampIndex.add(n)
                    flag=1
                    break
                n+=1
            if flag==0:
                precision_event.append("")
        self.lstFitnessPrecisionEvent=precision_event

    def detectStampedEvaluations(self):
        index=0
        l=[]
        for i in lfd.lstEvaluation:
            if i%self.__eval_stamp==0:
                l.append(self.lstIteration[index])
            index+=1
        self.lstStampedEvaluation=l

    def sampleEvents(self):
        e1=0.1
        e2=0.1
        sample=[]
        stamp=[]
        index=-1
        for i in self.lstEvaluation:
            index+=1
            flag=0
            if index in self.__setStampIndex:
                sample.append(self.lstIteration[index])
                flag=1
                
            if i%100000==0:
                if not flag:
                    sample.append(self.lstIteration[index])
                stamp.append(self.lstIteration[index])
        self.lstSample=sample
        self.lstStampedEvaluation=stamp
    def writeAnalyzeFile(self):
        fn="%s.Analyze%s" % os.path.splitext(self.filename)
#        fn="%s.Analyze" % self.filename
        f=open(fn,'w')
        f.write("DPE = %s\n" % self.lstDistancePrecisionEvent)
        f.write("SE = %s\n" % self.lstStampedEvaluation[0])
        f.write("FPE = %s\n" % self.lstFitnessPrecisionEvent)
        f.write("-----------------------\n")
        l=self.strInfo.split('\n')
        f.write("\n".join(l[:-4]))
        f.write("\n-----------------------\n")
        for i in self.lstSample:
            f.write("%s\n" % "\t".join(i))
        f.write("-----------------------\n")
        f.write("\n".join(l[-4:-1]))
        f.write("\n-----------\n")
        f.write(self.strAnswer)
        f.close()


def main():
    pass
    
if __name__ == "__main__":
    main()

